<?php


namespace App\Api\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;


class PromotionBargainList extends Model
{
    use HasFactory;
    protected $guarded = [];
    protected $table = 'cmc_promotion_bargain_list';

    /**
     * 模型关联 关联砍价商品
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function bargainGoods()
    {
       return  $this->belongsTo(PromotionBargainGoods::class,'bargain_id','id');
    }
    /**
     * 模型关联 关联商品
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function bargain()
    {
        return  $this->belongsTo(PromotionBargain::class,'bargain_id','id');
    }

    /**
     * 模型关联 关联用户
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function member()
    {
        return  $this->belongsTo(Member::class,'member_id','member_id');
    }

    /**
     * 获取用户关联砍价商品
     * @param $member_id
     * @return array
     */
    public static function userBargainGoods($memberId){

        return self::with('bargainGoods')->where('member_id',$memberId)->get()->toArray();
    }

    /**
     * 根据条件修改 或 添加新数据
     * @param $data
     * @param array $where
     * @return int
     */
    public static function saveData($data,$where = [])
    {
        if (!empty($where) && is_array($where)){
            return self::query()->where($where)->update($data);
        }
        return self::query()->insertGetId($data);
    }

    /**
     * 获取最新帮砍数据
     * @param $where
     * @param $filed
     * @return array
     */
    public static function barList($where,$filed)
    {
        return self::query()->where($where)->orderBy('old_price','asc')->first($filed)->toArray();
    }

    /**
     * 获取参与砍价活动次数
     * @param $where
     * @return int
     */
    public static function barCount($where)
    {
        return self::query()->where($where)->count();
    }

    /**
     * 获取砍价开始时间
     * @param $where
     * @param $filed
     * @return int
     */
    public static function startTime($where,$filed)
    {
        return self::query()->where($where)->value($filed);
    }


    public static function barInfo($where)
    {
        return self::with(['activity','member'])->where($where)->first()->toArray();
    }

}
